/* context.c
**
** Copyright (c) 2006, Damon Orecks <nomadxero@gmail.com>
** All rights reserved.
** 
** Redistribution  and  use  in  source  and  binary  forms,  with  or  without
** modification, are permitted provided that the following conditions are met:
** 
**  * Redistributions of  source code must retain the  above copyright notice,
**    this list of conditions and the following disclaimer.
** 
**  * Redistributions  in  binary  form  must reproduce  the  above  copyright
**    notice,  this list  of conditions  and the  following disclaimer  in the
**    documentation and/or other materials provided with the distribution.
** 
**  * Neither the name  of the Schmoo Group nor the  names of its contributors
**    may be  used to endorse or  promote products derived  from this software
**    without specific prior written permission.
** 
** THIS SOFTWARE IS PROVIDED BY  THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
** AND ANY  EXPRESS OR IMPLIED WARRANTIES,  INCLUDING, BUT NOT  LIMITED TO, THE
** IMPLIED WARRANTIES  OF MERCHANTABILITY AND FITNESS FOR  A PARTICULAR PURPOSE
** ARE DISCLAIMED.  IN NO  EVENT SHALL THE  COPYRIGHT OWNER OR  CONTRIBUTORS BE
** LIABLE  FOR  ANY  DIRECT,   INDIRECT,  INCIDENTAL,  SPECIAL,  EXEMPLARY,  OR
** CONSEQUENTIAL  DAMAGES  (INCLUDING,  BUT  NOT  LIMITED  TO,  PROCUREMENT  OF
** SUBSTITUTE GOODS  OR SERVICES;  LOSS OF USE,  DATA, OR PROFITS;  OR BUSINESS
** INTERRUPTION)  HOWEVER CAUSED  AND ON  ANY THEORY  OF LIABILITY,  WHETHER IN
** CONTRACT,  STRICT LIABILITY,  OR  TORT (INCLUDING  NEGLIGENCE OR  OTHERWISE)
** ARISING IN ANY WAY  OUT OF THE USE OF THIS SOFTWARE,  EVEN IF ADVISED OF THE
** POSSIBILITY OF SUCH DAMAGE.
**
*/

/* Macro Definitions */

/* File Includes */
#include <stdio.h>
#include <string.h>
#include "glib.h"
#include "xerobits.h"

/* Typedefs, Structs, Enums, Unions, etc. */

/* Function Prototypes */

static void xb_context_certificate_free( gpointer data, gpointer user_data );

/* Global Variable Declarations */
GQuark quark_xerobits;
GQuark quark_openssl;

/* Function Definitions */

/*!
** @brief      Initializes the xerobits context.
**
** @params[in] context  - a pointer to the XB_Context structure to initialize
** @params[in] error    - an error handle used to represent any errors.
**
** @returns    XB_ERR_NOERR - implies successful completion
** @returns    XB_ERR_INVALID_PARAM - a null pointer was passed as a parameter
*/

XB_Err XB_Context_Open( XB_Context *context, GError **error ) {
  XB_Err err = XB_ERR_NOERR;

  do {
    XB_BREAK_ON_NULL( context );

    quark_xerobits = g_quark_from_static_string( "xerobits" );
    quark_openssl  = g_quark_from_static_string( "openssl" );

    if( NULL == ( context->certificates = g_ptr_array_new() ) ) {
      XB_SET_ERROR( "failed allocating pointer array for certificates", NULL );
    }
  } while( 0 );

  return( err );
}

/*!
** @brief      Uninitializes the xerobits context (de-allocates global memory)
**
** @params[in] context  - a pointer to the XB_Context structure to initialize
**
** @returns    XB_ERR_NOERR - implies successful completion
** @returns    XB_ERR_INVALID_PARAM - a null pointer was passed as a parameter
*/

XB_Err XB_Context_Close( XB_Context *context ) {
  XB_Err err = XB_ERR_NOERR;

  do {
    XB_BREAK_ON_NULL( context );

    if( NULL != context->certificates ) {
      g_ptr_array_foreach( context->certificates, xb_context_certificate_free, NULL );
    }

    g_ptr_array_free( context->certificates, TRUE );
    
  } while( 0 );

  return( err );  
}

static void xb_context_certificate_free( gpointer data, gpointer user_data ) {
  XB_CertificateReference *cert = (XB_CertificateReference *) data;

  if( NULL != data ) {
    if( NULL != cert->issuer ) {
      free( cert->issuer );
    }

    if( NULL != cert->serial ) {
      free( cert->serial );
    }

    if( NULL != cert->pem ) {
      free( cert->pem );
    }

    if( NULL != cert->usage ) {
      free( cert->usage );
    }

    free( cert );
  }
}
